﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Bluyel.Core
{
    /// <summary>
    /// A generic list of data named: var and const.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ListDataGen<T>
        where T : IDataNamed
    {
        private string _name;

        /// <summary>
        /// Items of the list.
        /// </summary>
        private List<T> _listData = new List<T>();

        //=====================================================================
        #region Constructor.

        //---------------------------------------------------------------------
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="name"></param>
        public ListDataGen()
        {
            _name = "ListDataGen";
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="name"></param>
        public ListDataGen(string name)
        {
            _name = name;
        }
        #endregion

        //=====================================================================
        #region Properties.

        //---------------------------------------------------------------------
        public IEnumerable<T> ListData
        {
            get { return _listData; }
        }

        //---------------------------------------------------------------------
        public int Count
        { get { return _listData.Count; } }

        #endregion

        //=====================================================================
        #region Public Add/Update/Remove methods.

        //---------------------------------------------------------------------
        /// <summary>
        /// Add a new data, must be valid (id and name).
        /// 
        /// Check that the same is not already added.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public bool Add(T data)
        {
            if (data == null)
                return false;

            // check that the data is not already present
            if (_listData.Contains(data))
                return false;
            if (FindDataByName(data.Name) != null)
                return false;

            _listData.Add(data);
            return true;
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// Remove the data by the name.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool Remove(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
                return false;

            T d= FindDataByName(name);
            if(d==null)
                return false;

            return _listData.Remove(d);
        }

        /// <summary>
        /// remove the data, by the instance.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool Remove(T d)
        {
            if (d == null)
                return false;
            if (!_listData.Contains(d))
                return false;

            return _listData.Remove(d);
        }

        #endregion


        //=====================================================================
        #region Public Find methods.

        //---------------------------------------------------------------------
        public T FindDataByName(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
                // return null if name is wrong
                return default(T);

            return _listData.Find(d => d.Name.Equals(name,StringComparison.InvariantCultureIgnoreCase));
        }
        #endregion
    }
}
